package com.javabasic.basic_learning;

import java.util.HashMap;

/**
 * @Author xiongmin
 * @ClassName Test
 * @Description TODO
 * @Date 2019/8/7 12:26
 * @Version 1.0
 **/
public class Test {

    public static void main(String[] args) {
        Dog dog = new Dog();
        Dog dog1 = new Dog();
        dog.str = "hello";
        dog1.str = "hello";
        if (dog.equals(dog1)) {
            System.out.println("equals is true");
        } else {
            System.out.println("equals is false");
        }
        // 说明 == 比较的是内存地址而不是hashCode
        if (dog == dog1) {
            System.out.println("equals is true");
        } else {
            System.out.println("equals is false");
        }

        HashMap<Integer,Dog> hashMap = new HashMap<>();
        hashMap.put(1,dog);
        dog.str = "nihao";
        System.out.println(hashMap.get(1));
        Dog dog2 = dog;
        dog = null;
        System.out.println(hashMap.get(1));
        System.out.println(dog2);
    }
}
class Dog {
    String str;

    @Override
    public String toString() {
        return "Dog{" +
                "str='" + str + '\'' +
                '}';
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Dog dog = (Dog) o;
        return str.equals(dog.str);
    }

    @Override
    public int hashCode() {
        return 1;
    }
}
